﻿app.controller('UserRoomController', function ($scope, $rootScope, userRoomFactory, roomFactory, notificationFactory) {

    init();

    function init() {
        $scope.roleByGroup = JSON.parse(localStorage.getItem("menu"));
        $scope.userName = $rootScope.userName;
        //count--------------
        notificationFactory.countAddFriend(localStorage.getItem("idkey")).success(function (data) {
            $scope.countAddFriend = data;
        });
        notificationFactory.countFromRoom(localStorage.getItem("idkey")).success(function (data) {
            $scope.countFromRoom = data;
        });
        notificationFactory.countNewMessage(localStorage.getItem("idkey")).success(function (data) {
            $scope.countNewMessage = data;
        });
        $scope.searchRoomList = {};
        $('#listRoom').hide();
        $scope.formData = { Name: null, FullName: null, Description: null, CreateBy: localStorage.getItem("idkey") };
        userRoomFactory.GetRoomByUser(localStorage.getItem("idkey")).success(function (data) {
            $scope.roomByUser = data;
            angular.forEach(data, function (item) {
                if (item.Master == true)
                    $scope.hasMyRoom = true;
            });
        });
        $scope.joinRoom = function () {
            var id = $('#joinRoomInput').val();
            if (id != "") {
                var dataPost = {
                    UserId: localStorage.getItem("idkey"),
                    RoomId: id
                };
                userRoomFactory.postUserRoom(dataPost).success(function () {
                    alert('Đã gửi yêu cầu tham gia phòng!');
                });
            } else {
                alert("Hãy nhập Id của phòng mà bạn muốn tham gia!");
            }

        };
        $scope.search = function () {
            var dataPost = {
                SearchString: $('#searchRoomInput').val()
            }
            if ($('#searchRoomInput').val() != "") {
                $('#listsearch').hide();
                roomFactory.searchRoom(dataPost).success(function (data) {
                    $scope.searchRoomList = data;
                    $('#listsearch').fadeIn(500);
                });
            }

        };
        $('#listRoom').fadeIn(500);
        $scope.showRoomTaskClick = function () {
            $scope.roomTask = !$scope.roomTask;
        };
        $scope.createRoom = function() {
            roomFactory.postRoom($scope.formData).success(function(data) {
                if (data > 0) {
                    userRoomFactory.postUserRoom({
                        UserId: localStorage.getItem("idkey"),
                        RoomId: data,
                        Master: true
                    }).success(function() {
                        userRoomFactory.GetRoomByUser(localStorage.getItem("idkey")).success(function(d) {
                            $scope.roomTask = false;
                            $scope.roomByUser = d;
                            angular.forEach(d, function(item) {
                                if (item.Master == true)
                                    $scope.hasMyRoom = true;
                            });
                        });
                    });
                }
            });
        };



        createMenu();
    }
});